//
//  ZYCManageTourRecordDiscordViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/26.
//

#import "ZYCManageTourRecordDiscordViewController.h"
#import "ZYCTourRecordMarkCell.h"
#import "ZYCAttendanceEvaluateDetailCell.h"
#import "ZYCTourRecordsModel.h"
#import "ZYCTourRecordStatusPickerView.h"
#import "ZYCTourRecordStatusModel.h"
@interface ZYCManageTourRecordDiscordViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *comfirmButton;
@property (nonatomic, strong) ZYCTourRecordStatusPickerView *pickerView;
@property (nonatomic, strong) NSMutableArray *titles;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) ZYCTourRecordStatusModel *sModel;
@property (nonatomic, copy) NSString *classStatus;

@end

@implementation ZYCManageTourRecordDiscordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"异常";
     
    ZYCTourRecordStatusModel *model = [ZYCTourRecordStatusModel new];
    model.title = @"异常";
    model.status = @"4";
    model.textColor = HEX_RGB(@"#E93C06");

    self.sModel = model;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.comfirmButton];
    [self.view addSubview:self.tableView];
    self.classStatus = self.model.classStatus;
}

- (void)clickRightItem:(UIButton *)sender{
    [self modifyTourRecord];
}

- (void)modifyTourRecord{
    NSDictionary *params = @{
        @"checkId":self.model.checkId,
        @"classStatus":self.classStatus,
        @"classContent":self.model.classContent
    };
    [MrjSVProgressHUD showWithStatus:@"修改中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_modifyTourRecord requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:responseObject[@"body"]];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        ZYCTourRecordMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTourRecordMarkCell class]) forIndexPath:indexPath];
        if (indexPath.section == 0) {
            cell.model = self.sModel;
            
        }else{
            cell.isHid = YES;
            cell.statusLabel.textColor = HEX_RGB(@"#E93C06");
            cell.statusLabel.text = self.model.classStatusName;
        }
        cell.titleString = self.dataSource[indexPath.section];
        return cell;
    }else{
        ZYCAttendanceEvaluateDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCAttendanceEvaluateDetailCell class]) forIndexPath:indexPath];
        cell.titleString =  self.dataSource[indexPath.section];
        if (indexPath.section == 2) {
            cell.contentString = self.model.classContent;
        }else{
            cell.contentString = self.model.exceptContent;
        }
        cell.isHid = YES;
        return cell;
    }
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        [self.pickerView show];
    }
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTourRecordMarkCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTourRecordMarkCell class])];
        [_tableView registerClass:[ZYCAttendanceEvaluateDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCAttendanceEvaluateDetailCell class])];
        _tableView.estimatedRowHeight = 50;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
    }
    return _tableView;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [[UIButton alloc] init];
        [_comfirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _comfirmButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 15.0f);
        [_comfirmButton addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comfirmButton;
}

- (NSArray *)dataSource{
    if (!_dataSource) {
        _dataSource = @[@"选择标签:",@"巡堂情况:",@"说明:",@"异议:"];
    }
    return _dataSource;
}

- (ZYCTourRecordStatusPickerView *)pickerView{
    ZYCWeakSelf
    if (!_pickerView) {
        _pickerView = [[ZYCTourRecordStatusPickerView alloc] init];
        _pickerView.tipString = @"请选择状态";
        _pickerView.titles = self.titles;
        _pickerView.selectStatusBlock = ^(ZYCTourRecordStatusModel * _Nonnull model) {
            weakSelf.sModel = model;
            weakSelf.classStatus = model.status;
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [weakSelf.tableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationNone];
        };

        [self.view addSubview:_pickerView];
    }
    return _pickerView;
}

- (NSMutableArray *)titles {
    if (!_titles) {
        _titles = [NSMutableArray array];
        {
            ZYCTourRecordStatusModel *model = [ZYCTourRecordStatusModel new];
            model.title = @"正常";
            model.status = @"1";
            model.textColor = HEX_RGB(@"#39A85B");
            [_titles addObject:model];
        }
        
        {
            ZYCTourRecordStatusModel *model = [ZYCTourRecordStatusModel new];
            model.title = @"迟到";
            model.status = @"2";
            model.textColor = HEX_RGB(@"EB9F28");
            [_titles addObject:model];
        }
        
        {
            ZYCTourRecordStatusModel *model = [ZYCTourRecordStatusModel new];
            model.title = @"取消";
            model.status = @"3";
            model.textColor = HEX_RGB(@"#2B302D");
            [_titles addObject:model];
        }
        {
            ZYCTourRecordStatusModel *model = [ZYCTourRecordStatusModel new];
            model.title = @"异常";
            model.status = @"4";
            model.textColor = HEX_RGB(@"#E93C06");
            [_titles addObject:model];
        }

    }
    return _titles;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
